package com.yanqu.road.dao.impl.activity.pasture;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pasture.PastureAnimalData;
import com.yanqu.road.entity.activity.pasture.PastureGameData;
import com.yanqu.road.entity.activity.pasture.PastureUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class PastureUserDataDaoImpl extends TempDataObjectDao<PastureUserData> {

    public static String getPastureUserDataSql = "select * from `t_u_activity_pasture_user_data` where `activityId`=? and `userId`=?";

    public PastureUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getPastureUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertPastureUserDataSql = "insert into `t_u_activity_pasture_user_data`(activityId,userId,maxScore,todayTimes,animalData,gameData,practiceData,updateTime,resetTime,isBuySilverToken,isBuyGoldToken,recentStepsGameData,recentStepsPracticeData,gameCnt,maxRound,canRevive,practiceModelIdx,lastMaxScore,finishGuide) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(PastureUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTodayTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAnimalDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPracticeData())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBuySilverToken()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBuyGoldToken()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecentStepsGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecentStepsPracticeData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isCanRevive()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPracticeModelIdx()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMaxScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isFinishGuide()));
        if (getDb().executeNoneQuery(insertPastureUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updatePastureUserDataSql = "update `t_u_activity_pasture_user_data` set `maxScore`=?,`todayTimes`=?,`animalData`=?,`gameData`=?,`practiceData`=?,`updateTime`=?,`resetTime`=?,`isBuySilverToken`=?,`isBuyGoldToken`=?,`recentStepsGameData`=?,`recentStepsPracticeData`=?,`gameCnt`=?,`maxRound`=?,`canRevive`=?,`practiceModelIdx`=?,`lastMaxScore`=?,`finishGuide`=? where `activityId`=? and `userId`=?";

    @Override
    protected boolean updateData(PastureUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTodayTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getAnimalDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPracticeData())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getResetTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBuySilverToken()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isBuyGoldToken()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecentStepsGameData())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRecentStepsPracticeData())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGameCnt()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMaxRound()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isCanRevive()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPracticeModelIdx()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastMaxScore()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isFinishGuide()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updatePastureUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected PastureUserData parseData(ResultSet rs) throws SQLException {
        PastureUserData data = new PastureUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setMaxScore(rs.getLong("maxScore"));
        data.setTodayTimes(rs.getInt("todayTimes"));
        if (!StringUtils.isNullOrEmpty(rs.getString("gameData"))) {
            data.setGameData(JSON.parseObject(rs.getString("gameData"), new TypeReference<PastureGameData>() {
            }));
        } else {
            data.setGameData(new PastureGameData());
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("practiceData"))) {
            data.setPracticeData(JSON.parseObject(rs.getString("practiceData"), new TypeReference<PastureGameData>() {
            }));
        } else {
            data.setPracticeData(new PastureGameData());
        }
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setResetTime(rs.getLong("resetTime"));
        data.setBuySilverToken(rs.getBoolean("isBuySilverToken"));
        data.setBuyGoldToken(rs.getBoolean("isBuyGoldToken"));
        if (!StringUtils.isNullOrEmpty(rs.getString("animalData"))) {
            data.setAnimalDataMap(JSON.parseObject(rs.getString("animalData"), new TypeReference<Map<Integer, PastureAnimalData>>() {
            }));
        } else {
            data.setAnimalDataMap(new HashMap<>());
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("recentStepsGameData"))) {
            data.setRecentStepsGameData(JSON.parseObject(rs.getString("recentStepsGameData"), new TypeReference<LinkedList<PastureGameData>>() {
            }));
        } else {
            data.setRecentStepsGameData(new LinkedList<>());
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("recentStepsPracticeData"))) {
            data.setRecentStepsPracticeData(JSON.parseObject(rs.getString("recentStepsPracticeData"), new TypeReference<LinkedList<PastureGameData>>() {
            }));
        } else {
            data.setRecentStepsPracticeData(new LinkedList<>());
        }
        data.setGameCnt(rs.getInt("gameCnt"));
        data.setMaxRound(rs.getInt("maxRound"));
        data.setCanRevive(rs.getBoolean("canRevive"));
        data.setPracticeModelIdx(rs.getInt("practiceModelIdx"));
        data.setLastMaxScore(rs.getLong("lastMaxScore"));
        data.setFinishGuide(rs.getBoolean("finishGuide"));
        return data;
    }
}
